<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/23
 * Time: 11:20
 */
namespace Common\Logic;

use Common\Model\ArticleModel;
use Common\Model\BaseModel;
use Common\Util\ErrCode;
use Think\Upload;

class ArticleLogic {

    public function addArticle($params,$response) {

        $data = array(
            'title' => $params['title'],
            'content' => $params['content'],
            'channel_id' => $params['channel_id'],
            'keyword' => $params['keyword'] ? $params['keyword'] : '',
            'hot' => $params['hot'] ? $params['hot'] : '',
            'top' => $params['top'] ? $params['top'] : '',
            'description' => $params['description'] ? $params['description'] : '',
            'author' => $params['author'],
            'time' => time()
        );
        $ArticleModel = new ArticleModel();
        $res = $ArticleModel->add($data);
        if($res === false) {
            $response(ErrCode::SQL_ERROR);
        }
        return true;
    }

    public function getArticList($params,$response){

        $ArticleModel = new ArticleModel();

        $data = $ArticleModel ->getArticle($params['title'],C('PAGE_SIZE'));
        if($data === false){
            $response(ErrCode::SQL_ERROR);
        }
        return $data;
    }

    public function getArticleById($id,$response) {
        $ArticleModel = new ArticleModel();
        $data = $ArticleModel->getArticleById($id);
        if($data === false) {
            $response(ErrCode::SQL_ERROR);
        }
        return $data;
    }


    public function editArticle($params,$response) {

        $data = array(
            'id' => $params['id'],
            'title' => $params['title'],
            'content' => $params['content'],
            'channel_id' => $params['channel_id'],
            'keyword' => $params['keyword'] ? $params['keyword'] : '',
            'hot' => $params['hot'] ? $params['hot'] : '',
            'top' => $params['top'] ? $params['top'] : '',
            'description' => $params['description'] ? $params['description'] : '',
            'author' => $params['author'],
            'time' => time()
        );
        $ArticleModel = new ArticleModel();
        $res = $ArticleModel->save($data);
        if($res === false) {
            $response(ErrCode::SQL_ERROR);
        }
        return true;
    }


    public function dropArticle($params,$response){

        $data = array(
            'id' => array('in',$params['id']) ,
            'status' => BaseModel::STATUS_DROP
        );
        $ArticleModel = new ArticleModel();
        $res = $ArticleModel->save($data);
        if($res === false) {
            $response(ErrCode::SQL_ERROR);
        }
        return true;
    }
}